package mcom.xmks.bluetooth.mesh.dialog;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import mcom.xmks.bluetooth.mesh.R;
import mcom.xmks.bluetooth.mesh.core.BleDevice;

/**
 * 设备列表构造器
 */
public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.ViewHolder> {
     List<BleDevice> deviceList=null;
     //Map<String,Integer> rssiMap=null;
    OnClickListener listener;

     public DeviceListAdapter(OnClickListener listener){
         this.listener=listener;
         this.deviceList=new ArrayList<>();
         //this.rssiMap=new HashMap<>();
     }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_devicelist_item, parent, false);
        return new ViewHolder(item);
    }

    //@RequiresApi(api = Build.VERSION_CODES.R)
    @SuppressLint("MissingPermission")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BleDevice device=deviceList.get(position);
        if(device.getAlias()==null) {
            holder.name.setText(device.getName() == null ? "null" : device.getName());
        }else{
            holder.name.setText(device.getAlias());
        }
        holder.mac.setText(String.format(Locale.getDefault(), "%s",device.getId()));
        int rssi=device.getRssi();
        holder.rssi.setText(String.format(Locale.getDefault(),"%d dBm",rssi));
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(v -> {
            listener.onClick(deviceList.get((Integer) v.getTag()));
        });
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        //super.onBindViewHolder(holder, position, payloads);
        if(payloads.isEmpty()){
            onBindViewHolder(holder, position);
        }else{
            if(payloads.get(0)!=null){
                if(payloads.get(0) instanceof RSSIMSG){
                   RSSIMSG msg= (RSSIMSG) payloads.get(0);
                    holder.rssi.setText(String.format(Locale.getDefault(),"%d dBm",(msg.getRssi())));
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return deviceList==null?0:deviceList.size();
    }
    public void clear(){
        deviceList.clear();
        notifyDataSetChanged();
    }
    public void update( BleDevice device) {
        if(deviceList!=null){
            for(int i=0;i<deviceList.size();i++){
                BleDevice bleDevice=deviceList.get(i);
                //地址字符串比较（不考虑大小写）
                if(bleDevice.getAlias().equalsIgnoreCase(device.getAlias())){
                    notifyItemChanged(i, new RSSIMSG(bleDevice.getAlias(), device.getRssi()));
                    return;
                }
            }
            deviceList.add(device);
            notifyItemInserted(getItemCount());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name,mac,rssi;
        private ImageView indicator;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.name);
            mac=itemView.findViewById(R.id.mac);
            rssi=itemView.findViewById(R.id.rssi);
            indicator=itemView.findViewById(R.id.indicator);
        }
    }
    class RSSIMSG{
        private String address;
        private int rssi;
        public RSSIMSG(String address,int rssi){
            this.address=address;
            this.rssi=rssi;
        }
        public String getAddress() {
            return address;
        }
        public int getRssi() {
            return rssi;
        }
    }
    public interface OnClickListener{
        void onClick( BleDevice device);
    }
}
